package com.hyperfix.common.auth;

import com.hyperfix.common.exception.CustomException;
import com.hyperfix.common.holder.UserHolder;
import com.hyperfix.models.user.dtos.UserDTO;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * @author JiangChunYu
 * 2024/5/7
 */
@Aspect
@Component
public class RoleAuthorizationAspect {
    @Before("@annotation(requiresRole)")
    public void checkRoleAuthorization(RequiresRole requiresRole) {
        UserDTO userDTO = UserHolder.getUser();
        String userRole = userDTO.getRole();
        if (requiresRole.value().equals(userRole)) {
            return;
        }
        throw new CustomException("无权访问");
    }
}
